package com.animal.service.impl;

import com.animal.entity.AnAnimal;
import com.animal.entity.AnInvitation;
import com.animal.entity.AnOrder;
import com.animal.entity.AnUser;
import com.animal.mapper.*;
import com.animal.service.AnOrderService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author animal
 * @since 2022-01-28
 */
@Service
public class AnOrderServiceImpl extends ServiceImpl<AnOrderMapper, AnOrder> implements AnOrderService {
    @Autowired
    AnOrderMapper anOrderMapper;
    @Autowired
    AnUserMapper anUserMapper;
    @Autowired
    AnProductMapper anProductMapper;
    @Autowired
    AnAnimalMapper anAnimalMapper;
    @Autowired
    AnMeansMapper anMeansMapper;

    @Override
    public List<AnOrder> findAll(int size, int current) {
        Page<AnOrder> userPages = new Page<>(current,size);
        Page<AnOrder> userPagess=  anOrderMapper.selectPage(userPages,null);
        System.out.println(userPagess.getRecords());
        List<AnOrder> list=userPagess.getRecords();
        System.out.println("++++++++++++++++"+list.size());
        for(AnOrder a:list){
          AnUser anUser=anUserMapper.selectById(a.getUserId());
          QueryWrapper queryWrapper=new QueryWrapper();
          queryWrapper.eq("user_id",a.getUserId());
          anUser.setAnMeans(anMeansMapper.selectOne(queryWrapper));
          a.setAnUser(anUser);
            a.setAnProduct(anProductMapper.selectById(a.getPrId()));
            a.setAnAnimal(anAnimalMapper.selectById(a.getAnId()));
        }


        return list;
    }

    @Override
    public List<AnOrder> findorderByUserId(int size, int current, int userId) {
        Page<AnOrder> userPages = new Page<>(size,current);
        QueryWrapper<AnOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        Page<AnOrder> userPagess=  anOrderMapper.selectPage(userPages,wrapper);
        List<AnOrder> list=userPagess.getRecords();
        for(AnOrder a:list){
            AnUser anUser=anUserMapper.selectById(a.getUserId());
            QueryWrapper queryWrapper=new QueryWrapper();
            queryWrapper.eq("user_id",a.getUserId());
            anUser.setAnMeans(anMeansMapper.selectOne(queryWrapper));
            a.setAnUser(anUser);
            if(a.getPrId()!=null){
                a.setAnProduct(anProductMapper.selectById(a.getPrId()));
            }

            if(a.getAnId()!=null){
                a.setAnAnimal(anAnimalMapper.selectById(a.getAnId()));
            }

        }
        return list;
    }
}
